package com.db;

	import java.sql.PreparedStatement;
	import java.sql.ResultSet;
	import java.sql.SQLException;
	import java.sql.Statement;
	import java.util.LinkedList;
	import java.util.List;

	public class GroupsDAO extends AbstractDAO <Groups> {

		
		private Statement stm = null;
		private PreparedStatement prepstm = null;
		private ResultSet rs = null;
		
		
		@Override
		public void add (Groups entity) {
			
			try
			{
			connection = getConnection();
			prepstm = connection.prepareStatement("INSERT INTO group VALUES (?, ?, ?)");
			prepstm.setInt(1, entity.getId());
			prepstm.setString(2, entity.getName());
			prepstm.setInt(3, entity.getOfficer_id());
			prepstm.execute();
			}
			
			catch (Exception e)
			{
				e.printStackTrace();
			}
			
			
		}

		@Override
		public List <Groups> getAll() {
			
			List <Groups> groupList = new LinkedList <Groups> ();
			
			try {
				connection = getConnection();
				stm = connection.createStatement();
				rs = stm.executeQuery("SELECT * FROM group");
				
				while (rs.next())
				{
					Groups group = new Groups (rs.getInt(1), rs.getString(2), rs.getInt(3));
					groupList.add(group);	
				}
			}
			catch (SQLException e) {	
				e.printStackTrace();
			}
			
			closeConnection();
			return groupList;
		}

		
		@Override
		public void delete(int id) {
		
			try {
				connection = getConnection();
				prepstm = connection.prepareStatement("DELETE * FROM groups WHERE id = ?");
				prepstm.setInt(1, id);
				prepstm.execute();
				
			} 
			catch (SQLException e) {
				e.printStackTrace();
			}
			
		}

		
		@Override
		public Groups getById (int id) throws SQLException {
			
			Groups gr = null;
			
			try
			{
				connection = getConnection();
				prepstm = connection.prepareStatement("SELECT * FROM group WHERE id = ?");
				prepstm.setInt(1, id);
				rs = prepstm.executeQuery();
				
			}
			
			catch (Exception e)
			{
				e.printStackTrace();
			}
			
			if (rs.next())
			{
				gr = new Groups (rs.getInt(1), rs.getString(2), rs.getInt(3));
			}
			
			return gr;
		}

		
		
}


